package Client;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;

import ServerApp.IServer;
import ServerApp.Meeting;
import ServerApp.Tools;
import ServerApp.User;

@SuppressWarnings("serial")
public class NewMeeting extends JDialog {

	private IServer server;
	private Date selectedDate;
	private JLabel lTitle = new JLabel("New Meeting", JLabel.CENTER);
	private JLabel lName = new JLabel("Meeting name: ");
	private JTextField tName = new JTextField();
	private JLabel lDate1 = new JLabel("Date: ");
	private JLabel lDate2 = new JLabel("Date: ");
	private JSpinner timeSpinner = new JSpinner(new SpinnerDateModel());
	private JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(
			timeSpinner, "HH:mm");
	private JLabel lTime = new JLabel("Time: ");
	private JLabel lLocation = new JLabel("Location: ");
	private JTextField tLocation = new JTextField();
	private JLabel lDetails = new JLabel("Details: ");
	private JTextArea tDetails = new JTextArea();
	private JScrollPane scrollPane;
	private JButton bnOK = new JButton("   OK   ");
	private JButton bnCancel = new JButton("Cancel");
	private JPanel pnButtons = new JPanel();
	private User currentUser;
	private JFrame parent;
	
	public NewMeeting(JFrame parent, Date date, IServer server, User user) {
		super(parent, true);
		this.currentUser = user;
		this.parent = parent;
		this.server = server;
		this.selectedDate = date;
		lTitle.setFont(new Font("SansSerif", Font.BOLD, 16));

		timeSpinner.setEditor(timeEditor);
		timeSpinner.setValue(date);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		String strDate = cal.get(Calendar.DAY_OF_MONTH) + "."
				+ (cal.get(Calendar.MONTH) + 1) + "." + cal.get(Calendar.YEAR);
		lDate2.setText(strDate);
		// tDetails.setFont(new Font("Serif", Font.ITALIC, 16));
		tDetails.setLineWrap(true);
		tDetails.setWrapStyleWord(true);
		scrollPane = new JScrollPane(tDetails);
		scrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setPreferredSize(new Dimension(250, 250));

		bnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				bnOKAction();
			}
		});

		bnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bnCancelAction();
			}
		});

		pnButtons.add(bnOK);
		pnButtons.add(bnCancel);

		this.getContentPane().setLayout(new GridBagLayout());

		this.getContentPane().add(
				lTitle,
				new GridBagConstraints(0, 0, 2, 1, 1.0, 1.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));

		this.getContentPane().add(
				lName,
				new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
						GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
						new Insets(3, 6, 3, 3), 0, 0));
		this.getContentPane().add(
				tName,
				new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
						GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
						new Insets(3, 3, 3, 6), 0, 0));
		this.getContentPane().add(
				lDate1,
				new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
						GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
						new Insets(3, 6, 3, 3), 0, 0));
		this.getContentPane().add(
				lDate2,
				new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
						GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
						new Insets(3, 3, 3, 6), 0, 0));
		this.getContentPane().add(
				lTime,
				new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
						GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
						new Insets(3, 6, 3, 3), 0, 0));
		this.getContentPane().add(
				timeSpinner,
				new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
						GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
						new Insets(3, 3, 3, 6), 0, 0));
		this.getContentPane().add(
				lLocation,
				new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0,
						GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
						new Insets(3, 6, 3, 3), 0, 0));
		this.getContentPane().add(
				tLocation,
				new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
						GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
						new Insets(3, 3, 3, 6), 0, 0));
		this.getContentPane().add(
				lDetails,
				new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0,
						GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
						new Insets(3, 6, 3, 3), 0, 0));
		this.getContentPane().add(
				scrollPane,
				new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0,
						GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
						new Insets(3, 3, 3, 6), 0, 0));
		this.getContentPane().add(
				pnButtons,
				new GridBagConstraints(0, 6, 2, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.NONE,
						new Insets(3, 6, 3, 3), 0, 0));
		// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.pack();
		// this.setSize(400, 300);
		this.setResizable(false);
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(screen.width / 2 - this.getWidth() / 2, screen.height
				/ 2 - this.getHeight() / 2);
	}

	private void bnOKAction() {
		String errMsg = "";
		if (tName.getText().isEmpty())
			errMsg += "Invalid meeting name\n";

		Calendar c1 = Calendar.getInstance();
		c1.setTime(((SpinnerDateModel) timeSpinner.getModel()).getDate());
		Calendar c2 = Calendar.getInstance();
		c2.setTime(selectedDate);
		c2.set(Calendar.HOUR, c1.get(Calendar.HOUR));
		c2.set(Calendar.MINUTE, c1.get(Calendar.MINUTE));

		if (tLocation.getText().isEmpty())
			errMsg += "Invalid meeting location\n";

		if (!errMsg.isEmpty())
			Tools.errorMessage("Error", errMsg);
		else {
			try {
				if(!server.addNewMeeting(new Meeting(tName.getText(), tLocation
						.getText(), c2.getTime(), tDetails.getText()), currentUser)){
					Tools.errorMessage("Timeout", "Your session has timed out. please login again.");
					server.logOut(currentUser);
					this.dispose();
					parent.dispose();
					new LoginFenster().setVisible(true);
				}
			} catch (RemoteException e) {
				Tools.errorMessage("Error", e.getMessage());
			}
			this.dispose();
		}
	}

	private void bnCancelAction() {
		this.dispose();
	}

	// public static void main(String[] args) {
	// NewMeeting meet = new NewMeeting("test", null);
	// meet.setVisible(true);
	// }
}
